package ldh.im.fxbase.richtext;

import ldh.im.fxbase.richtext.item.*;
import org.fxmisc.richtext.model.Codec;
import org.fxmisc.richtext.model.NodeSegmentOpsBase;

import java.io.File;

public enum TextItemType {
    emoji(new EmojiTextItemOps(), EmojiTextItem.createCodec(), "e"),
    image(new ImageTextItemOps(), ImageTextItem.createCodec(), "i"),
    audio(new AudioTextItemOps(), AudioTextItem.createCodec(), "a"),
    video(new VideoTextItemOps(), VideoTextItem.createCodec(), "v"),
    file(new FileTextItemOps(), ImageTextItem.createCodec(), "f"),
    ;

    private NodeSegmentOpsBase textItemOps;
    private String value;
    private Codec<? extends TextItem> codec;

    private TextItemType(NodeSegmentOpsBase textItemOps, Codec<? extends TextItem> codec, String value) {
        this.textItemOps = textItemOps;
        this.codec = codec;
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public NodeSegmentOpsBase getTextItemOps() {
        return textItemOps;
    }

    public <O extends TextItem> O newTextItem(String data) {
        if (this == emoji) {
            return (O) new EmojiTextItem(data);
        } else if (this == image) {
            return (O) new ImageTextItem(data);
        } else if (this == file) {
            return (O) new FileTextItem(new File(data));
        } else if (this == audio) {
            return (O) new AudioTextItem(new File(data));
        } else if (this == video) {
            return (O) new VideoTextItem(new File(data));
        }
        throw new RuntimeException("type: " + this.name() + "不支持");
    }

    public static TextItemType parse(String value) {
        for (TextItemType textItemType : TextItemType.values()) {
            if (textItemType.name().equals(value) || textItemType.value.equals(value)) {
                return textItemType;
            }
        }
        throw new RuntimeException("type: " + value + "不支持");
    }

}
